package servlet.goods;

import bean.Area;
import bean.Goods;
import bean.People;
import bean.Status;
import dao.GoodsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by young on 2017/6/26.
 */
@WebServlet(name = "AddGoods", urlPatterns = "/addGoods")
public class AddGoods extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setHeader("content-type", "text/html;charset=UTF-8");
        Goods goods = new Goods();
        goods.setName(request.getParameter("goodsName"));
        goods.setSummary(request.getParameter("goodsSummary"));
        People people = new People();
        people.setId(Integer.parseInt(request.getParameter("peopleId")));
        goods.setPeople(people);
        Area area = new Area();
        area.setId(Integer.parseInt(request.getParameter("areaId")));
        goods.setArea(area);
        Status status = new Status();
        status.setId(Integer.parseInt(request.getParameter("statusId")));
        goods.setStatus(status);
        GoodsDao goodsDao = new GoodsDao();
        int row = goodsDao.addGoods(goods);
        if (row != 0) {
            response.sendRedirect("index.jsp");
        } else {
            response.sendRedirect("errorPage.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
